package com.miaosmart.controller;

import com.miaosmart.common.convention.result.Result;
import com.miaosmart.common.convention.result.Results;
import com.miaosmart.dto.req.EmailVerficationReqDTO;
import com.miaosmart.dto.req.LoginReqDTO;
import com.miaosmart.dto.req.RegisterReqDTO;
import com.miaosmart.dto.req.UserUpdateReqDTO;
import com.miaosmart.dto.resp.GetUserInfoRespDTO;
import com.miaosmart.dto.resp.LoginRespDTO;
import com.miaosmart.dto.resp.RegisterRespDTO;
import com.miaosmart.dto.resp.UserUpdateRespDTO;
import com.miaosmart.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;


/**
 * @Author: miaomaster
 * @Description: 用户控制器
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {
    
    private final AuthService authService;


    /**
     * @param email
     * @return {@code Result<Void> }
     * @Description: 发送邮箱验证码
     */

    @PostMapping("/send-verification-code")
    public Result<Void> sendVerificationCode(@RequestBody EmailVerficationReqDTO email) {
        authService.sendVerificationCode(email);
        return Results.success();
    }

    /**
     * @param registerDTO
     * @return {@code Result<Void> }
     * @Description: 用户注册
     */

    @PostMapping("/register")
    public Result<RegisterRespDTO> register(@RequestBody @Validated RegisterReqDTO registerDTO) {

        return Results.success(authService.register(registerDTO));
    }

    /**
     * @param loginDTO
     * @return {@code Result<LoginRespDTO> }
     * @Description: 用户登录
     */

    @PostMapping("/login")
    public Result<LoginRespDTO> login(@RequestBody @Validated LoginReqDTO loginDTO) {
        return Results.success(authService.login(loginDTO));
    }

    @GetMapping("/user/info")
    public Result<GetUserInfoRespDTO> getUserInfo() {
        return Results.success(authService.getCurrentUser());
    }

    /**
     * @param userUpdateDTO
     * @return {@code Result<UserUpdateRespDTO> }
     * @Description: 更新用户信息
     */

    @PostMapping("/user/update")
    public Result<UserUpdateRespDTO> updateUserInfo(@ModelAttribute @Validated UserUpdateReqDTO userUpdateDTO) throws IOException {
        return Results.success(authService.updateUser(userUpdateDTO));
    }
} 